<!DOCTYPE html>
<html>
<head>
	<?php
require_once("../inc/constants.inc.php");
require_once("../inc/connection.php");
session_start();
?>
</head>
<body>
<?php
	$questionTable = "question_table";
	if(isset($_POST['backToDashboard']))
		{
			$url="../dashboard.php";
			header("Refresh:0;URL=$url");
			exit(0);
		}

	if(isset($_GET['hiddenQuestionId']))
		{
			$questionNumber = $_GET['hiddenQuestionId']; 
		}
	elseif(isset($_POST['hiddenQuestionId']))
		{
			$questionNumber = $_POST['hiddenQuestionId']; 
		}
	echo $questionNumber;
	//exit(0);
	
			/*
			*
			*	For adding New questions
			*
			*	$_POST['questionSubmitButton'] -- from add-questions.php
			*
			*/
	
	
			if(isset($_POST['questionSubmitButton']))
				{
				
			
			$question = mysql_real_escape_string($_POST['questionTextArea']);			
			$optionOne = mysql_real_escape_string($_POST['optionOne']);
			$optionTwo = mysql_real_escape_string($_POST['optionTwo']);
			$optionThree = mysql_real_escape_string($_POST['optionThree']);
			$optionFour = mysql_real_escape_string($_POST['optionFour']);
			$correctOption = $_POST['optionRadio'];
			$questionTable = "question_table";
						
			$questionType = $_SESSION['test_type'];
			//$questionType = $_POST['hiddenId'];   //$_POST['questionTypeId']; ------------have to change
			
		// Validating questions ---- functions.php
		validateQuestion($questionTable, $question, $optionOne, $optionTwo, $optionThree, $optionFour, $_POST['optionRadio'], $questionNumber);
		
		echo $_POST['optionRadio'];
		//exit(0);
		
			if($_POST['optionRadio']=="a")
				{
					$correctAnswer = $optionOne;
				}
			elseif($_POST['optionRadio']=="b")
				{
					$correctAnswer = $optionTwo;
				}
			elseif($_POST['optionRadio']=="c")
				{
					$correctAnswer = $optionThree;
				}
			elseif($_POST['optionRadio']=="d")
				{
					$correctAnswer = $optionFour;
				}	

			echo $correctAnswer;
			
			$sql = "insert into `question_table` (`typeId`, `question`, `correctAnswer`, `correctOption`, `optionOne`, `optionTwo`, `optionThree`, `optionFour`) values ('$questionType','$question','$correctAnswer','$correctOption','$optionOne','$optionTwo', '$optionThree', '$optionFour')";

			if(mysql_query($sql))
					{
						
						$url="../add-question.php?isAdded=yes&selectType=$questionType";
						header("Refresh:0;URL=$url");
					}
					else
					{
						
						$url="../add-question.php?isAdded=no";
						header("Refresh:0;URL=$url");
					}
					
				}
				
				
		

			/*
			*
			*	For Updating questions
			*
			*	$_POST['questionUpdateButton'] -- from add-questions.php
			*
			*/		
				
			elseif(isset($_POST['questionUpdateButton']))
				{
				
			$questionTable = "question_table";
			$question = mysql_real_escape_string($_POST['questionTextArea']);			
			$optionOne = mysql_real_escape_string($_POST['optionOne']);
			$optionTwo = mysql_real_escape_string($_POST['optionTwo']);
			$optionThree = mysql_real_escape_string($_POST['optionThree']);
			$optionFour = mysql_real_escape_string($_POST['optionFour']);
			$correctOption = $_POST['optionRadio'];
			
			$questionType = $_SESSION['test_type'];
			
			
			//$questionType = 1;   //$_POST['questionTypeId']; ------------have to change
	
		// validating question form ----- functions.php
	validateQuestion($questionTable, $question, $optionOne, $optionTwo, $optionThree, $optionFour, $_POST['optionRadio'], $questionNumber);
		

		
			if($_POST['optionRadio']=="a")
				{
					$correctAnswer = $optionOne;
				}
			elseif($_POST['optionRadio']=="b")
				{
					$correctAnswer = $optionTwo;
				}
			elseif($_POST['optionRadio']=="c")
				{
					$correctAnswer = $optionThree;
				}
			elseif($_POST['optionRadio']=="d")
				{
					$correctAnswer = $optionFour;
				}	
			
			$questionNumber = $_POST['hiddenQuestionId'];
				echo $questionNumber;
				echo $question;
				echo $correctAnswer . $optionOne  . $optionTwo . $optionThree . $optionFour;
				//exit(0);
				
				
				//$q = "UPDATE `$questionTable` SET typeId=343 WHERE questionNumber=$questionNumber";
				
				
					$q = "UPDATE `$questionTable` SET typeId='$questionType', question='$question', correctAnswer='$correctAnswer', correctAnswer='$correctOption', optionOne='$optionOne', optionTwo='$optionTwo', optionThree='$optionThree', optionFour='$optionFour'  WHERE questionNumber=$questionNumber";
					
					
					//$q = "UPDATE `$tableName` SET image_name='$filename', page='$content' WHERE id='$noOfPages'";
				   
				   if(mysql_query($q))
					{
						//echo $questionNumber;
						//exit(0);
						if(isset($_POST['fromViewList']))
							{
								header( "Location: ../question-list.php?isUpdated=yes&typeId=$questionType" );
							}
						else
							{
								header( "Location: ../add-question.php?isUpdated=yes&selectType=$questionType" );
							}
					}

					else
					{
						if(isset($_POST['fromViewList']))
							{
								header( "Location: ../question-list.php?isUpdated=no" );
							}
						else
							{
								header( "Location: ../add-question.php?isUpdated=no" );
							}
					}	
				}
				elseif(isset($_GET['deleteQuestionId']) && isset($_GET['deleteQuestionId']))
				{
					if(mysql_query("DELETE FROM `$questionTable` WHERE questionNumber = '$_GET[deleteQuestionId]'"))
						{
							if(isset($_GET['fromList'])=="yes")				//From question-list.php
								{
									$typeId = $_GET['selectType'];
									$url="../question-list.php?isDeleted=yes&typeId=$typeId";					
									header("Refresh:0;URL=$url");
								}
							else
								{
									$questionType = $_GET['selectType'];
									$url="add-question.php?isDeleted=yes&selectType=$questionType";					
									header("Refresh:0;URL=$url");
								}							
						}
				}
				
				//$questionTable = "question_table";
		//}

	function validateQuestion($questionTable, $questionTextArea, $optionOnefunc, $optionTwofunc, $optionThreefunc, $optionFourfunc, $optionRadio, $questionNumber)
	{
	

		//$_SESSION['adminQuestionType'];
		$typeId = $_SESSION['test_type'];
		
		$questionID = $questionNumber;
	//$questionTable = "question_table";

			$question = addslashes(mysql_real_escape_string($questionTextArea));			
			$optionOne = addslashes(mysql_real_escape_string($optionOnefunc));
			$optionTwo = addslashes(mysql_real_escape_string($optionTwofunc));
			$optionThree = addslashes(mysql_real_escape_string($optionThreefunc));
			$optionFour = addslashes(mysql_real_escape_string($optionFourfunc));

			if(empty($question))
			{
				if(isset($_POST['fromViewList']))
					{
						$url="../question-list.php?isEmpty=yes&one=$optionOne&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID&typeId=$questionType&showField=yes";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
				else
					{
						$url="../add-question.php?isEmpty=yes&one=$optionOne&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
			}
			elseif(empty($optionOne))
			{
				if(isset($_POST['fromViewList']))
					{
						$url="../question-list.php?isEmpty=yes&question=$question&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID&typeId=$questionType&showField=yes";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
				else
					{
						$url="../add-question.php?isEmpty=yes&question=$question&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";					
						header("Refresh:0;URL=$url");
						exit(0);
					}
			}
			elseif(empty($optionTwo))
			{
				if(isset($_POST['fromViewList']))
					{
						$url="../question-list.php?isEmpty=yes&question=$question&one=$optionOne&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID&typeId=$questionType&showField=yes";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
				else
					{
						$url="../add-question.php?isEmpty=yes&question=$question&one=$optionOne&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";					
						header("Refresh:0;URL=$url");
						exit(0);
					}
			}
			elseif(empty($optionThree))
			{
				if(isset($_POST['fromViewList']))
					{
						$url="../question-list.php?isEmpty=yes&question=$question&two=$optionTwo&one=$optionOne&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID&typeId=$questionType&showField=yes";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
				else
					{
						$url="../add-question.php?isEmpty=yes&question=$question&two=$optionTwo&one=$optionOne&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";					
						header("Refresh:0;URL=$url");
						exit(0);
					}
			}
			elseif(empty($optionFour))
			{
				if(isset($_POST['fromViewList']))
					{
						$url="../question-list.php?isEmpty=yes&question=$question&two=$optionTwo&three=$optionThree&one=$optionOne&selectType=$typeId&hiddenQuestionId=$questionID&typeId=$questionType&showField=yes";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
				else
					{
						$url="../add-question.php?isEmpty=yes&question=$question&two=$optionTwo&three=$optionThree&one=$optionOne&selectType=$typeId&hiddenQuestionId=$questionID";					
						header("Refresh:0;URL=$url");
						exit(0);
					}
			}
			elseif(empty($optionRadio))
			{
				if(isset($_POST['fromViewList']))
					{
						$url="../question-list.php?isEmpty=yes&isEmptyCheck=yes&question=$question&one=$optionOne&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID&typeId=$questionType&showField=yes";			
						header("Refresh:0;URL=$url");
						exit(0);
					}
				else
					{
						$url="../add-question.php?isEmpty=yes&isEmptyCheck=yes&question=$question&one=$optionOne&two=$optionTwo&three=$optionThree&four=$optionFour&selectType=$typeId&hiddenQuestionId=$questionID";
						header("Refresh:0;URL=$url");
						exit(0);
					}
			}
						
	}	
		
		
		
?>

</body>
</html>